package com.wenx.ftpcamel;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by wenx on 2019-07-22 10:07
 */
public class FileTest44 {

    public static void main(String[] args) throws Exception {
        ModelCamelContext camelContext = new DefaultCamelContext();
        camelContext.start();
        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                LocalDateTime start = LocalDateTime.now();
                System.out.println("开始 "+ start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                from("quartz://825da6a0-7a77-49bf-8bbe-0301f0ee6957?cron=0 * * * * ? *&stateful=true")
                        .doTry()
                            .to("http://localhost:8088/rest/api/getFileInputStream")
                            .process(exchange -> {
                                LocalDateTime get = LocalDateTime.now();
                                System.out.println("读取文件完成 "+ get.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                                exchange.getOut().setBody(exchange.getIn().getBody());
                                exchange.setProperty("num",3);
                            })
                            .loopDoWhile(exchange -> {
                                Integer num = exchange.getProperty("num", Integer.class);
                                return num > 0;
                            })
                                .process(exchange -> {
                                })
                                .to("http://localhost:8088/rest/api/getFileInputStream")
                                .process(exchange -> {
                                    Integer num = exchange.getProperty("num", Integer.class);
                                    num = num - 1;
                                    exchange.setProperty("num",num);
                                })
                            .end()
                        .endDoTry()
                            .doCatch(Exception.class)
                            .process(exchange -> {})
                        .doFinally()
                            .process(exchange -> {
                                LocalDateTime get = LocalDateTime.now();
                                System.out.println("写入文件完成 "+ get.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                            });
            }
        });

        synchronized (FileDemo1.class) {
            FileDemo1.class.wait();
        }
    }

}
